package model.inventory;

import java.util.List;

import model.Container;
import model.Player;
import model.Preference;
import model.contents.Content;
import model.contents.ContentType;
import util.PlayerUtilities;

public class InventoryTable extends Content {
	private InventoryTableDefinition definition;
	private String[] keys;

	public InventoryTable(InventoryTableDefinition def, ContentType type) {
		super(-1, type, "", false, 0);
		this.definition = def;
		this.keys = InventoryPlayer.getInstance().getInventoryItemsKeys();
	}

	public Object[][] getVisibleRows() {
		Object[][] data = new Object[this.definition.getNumberOfTableRows()][this.definition.getTableFields().length];
		int startingIndex = getStartingIndex();
		if (InventoryPlayer.getInstance().getNumRecords() <= 0) {
			return data;
		}
		
		//System.out.println("showing table from row " + startingIndex + ", " +data.length + " rows");
		
		int recordIndex = 0;
		for (int i = 0; i < data.length; i++) {
			recordIndex = (startingIndex + i) % InventoryPlayer.getInstance().getNumRecords();
			// assert recordIndex < number of records
			InventoryItem item = InventoryPlayer.getInstance().getItem(keys[recordIndex]);
			for (int j = 0; j < data[0].length; j++) {
				String fieldName = definition.getTableFields()[j];
				data[i][j] = item.get(fieldName);
			}
		}
		return data;
	}

	public int getStartingIndex() {
		return Math.abs((InventoryPlayer.getInstance().getPosition()) + 1) % InventoryPlayer.getInstance().getNumRecords();
	}

	public List<InventoryRecord> getInventoryRecords() {
		int startingIndex = getStartingIndex();
		
		//System.out.println("MIN = "+Math.max(InventoryPlayer.getInstance().inventoryRecords.size(),
			//	startingIndex + definition.getNumberOfTableRows()));
		
		List<InventoryRecord> firstPortion = InventoryPlayer.getInstance().inventoryRecords.subList(0,startingIndex);
		//Collections.reverse(firstPortion);
		
		System.out.println("Total list size "+InventoryPlayer.getInstance().inventoryRecords.size());
		System.out.println("Number of rows "+definition.getNumberOfTableRows());
		
		List<InventoryRecord> list;
		
		list = InventoryPlayer.getInstance().inventoryRecords.subList(startingIndex, Math.min(InventoryPlayer.getInstance().inventoryRecords.size(),
				startingIndex + definition.getNumberOfTableRows()));
		
		System.out.println("List portion size sending before "+list.size());
		if(list.size()<definition.getNumberOfTableRows())
		{
			/*
			switch(list.size())
			{
				case 4:
					firstPortion = firstPortion.subList(0,1);
					break;
				case 3:
					firstPortion = firstPortion.subList(1,3);
					break;
				case 2:
					firstPortion = firstPortion.subList(2,5);
					break;
				case 1:
					firstPortion = firstPortion.subList(3,7);
					break;
			}
			*/
			firstPortion = firstPortion.subList(0, list.size());
			list.addAll(firstPortion);
			/*
			int rm = 5 - list.size();
			for(int i = 0 ; i < rm; i++)
			{
				list.add(rm+i,firstPortion.get(i));
				System.out.println("doing");
			}
			*/
		}
		
		System.out.println("List portion size sending "+list.size());
		
		//System.exit(1);
		return list;
	}

	public Object[] getFields() {
		return definition.getTableFields();
	}

	public InventoryTableDefinition getDefinition() {
		return this.definition;
	}

	public Container getContainer() {
		return Player.getInstance().getContainer(this.definition.getTableContainerId());
	}

	public int getNumRows() {
		return this.definition.getNumberOfTableRows();
	}

	public int getRowHeight() {
		return PlayerUtilities.scale(this.definition.getTableRowHeight(), Preference.getInstance().getScale());
	}
}
